package com.lin.manager.config;

import cn.hutool.core.collection.CollectionUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

/**
 * @作者: 林江
 * @创建时间: 2024/3/11
 * @功能:
 */
@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {

    @Bean
    public Docket groupRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(groupApiInfo())
                .groupName("admin-service")
                .select()
//                .apis(RequestHandlerSelectors.basePackage("com.lin.manager.controller"))
//                .paths(PathSelectors.any())
                // 自定义排除一些控制层
                .paths(path -> {
                    List<String> excludeUrl = CollectionUtil.newArrayList("/login", "/alipay/notify_url", "/wxpay/notify_url", "/error");
                    return !excludeUrl.stream().anyMatch(url -> url.equals(path));
                })
                .build()
                .globalOperationParameters(getGlobalOperationParameters())
                ;
    }

    private ApiInfo groupApiInfo() {
        return new ApiInfoBuilder()
                .title("java system")
                .description("后台管理系统")
                .termsOfServiceUrl("http://localhost:9000")
                .contact("linjiang@qq.com")
                .version("1.0").build();
    }

    /**
     * 全局参数
     *
     * @return
     */
    private List<Parameter> getGlobalOperationParameters() {
        List<Parameter> list = new ArrayList<>();
        list.add(new ParameterBuilder()
                .name(HttpHeaders.AUTHORIZATION)
                .description("访问令牌")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build());
        return list;
    }
}
